<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller{
	public function index(){
        $this->setStyle('index');
        $m_cate = D('Systemht/Cate');
        $m_article = M('Article');
        //产品栏目
        $cate_list = $m_cate->field(array('id','name'))->where(array('parent_id'=>1))->order('sort ASC')->select();
        foreach($cate_list as $val){
            $pro_id[] = $val['id'];
        }
        $pro_list = $m_article->where(array(
            'status' => 1,
            'publish_time' => array('lt',time()),
            'recom' => 1,
            'cate_id' => array('in',implode(',', $pro_id))
            ))->order('publish_time DESC')->limit(12)->select();
       
        //新闻中心
        $news_id = $m_cate->getCateId(2);
        $news_list = $m_article->where(array(
            'status' => 1,
            'publish_time' => array('lt',time()),
            'cate_id' => array('in',implode(',', $news_id))
            ))->order('publish_time DESC')->limit(18)->select();
        $this->assign(array(
            'cate_list' => $cate_list,
            'pro_list' => $pro_list,
            'news_list' => $news_list,
        ));
		$this->display();
	}
	public function _initialize(){
    	//栏目列表
    	$m_cate = D('Systemht/Cate');
    	$cate = $m_cate->where(array('parent_id'=>0,'status'=>1))->order('sort ASC')->select();
    	$this->assign('cate',$cate);

    	//获取网页配置信息
    	$config = M('Config')->where(array('id'=>1))->find();
    	$this->assign('config',$config);

        //轮播图片
        $m_pict = M('Pict');
        $pict = $m_pict->where(array('cate_id'=>1))->order('sort ASC')->limit(3)->select();
        $this->assign('pict',$pict);
         //推荐产品
        $pro_id = $m_cate->getCateId(3);
        $pro_list = M('Article')->field(array('id','image','name'))->where(array(
                'status' => 1,
                'publish_time' => array('lt',time()),
                'cate_id' => array('in',implode(',', $pro_id)),
                'recom' => 1
            ))->order('sort DESC,publish_time DESC')->limit(10)->select();
        $this->assign('pro_list',$pro_list);

        //站内公告
        $gonggao = M('Article')->where(array(
            'status' => 1,
            'publish_time' => array('lt',time()),
            'cate_id' => 14
            ))->order('publish_time DESC')->limit(10)->select();
        $this->assign('gonggao',$gonggao);
    }

    //验证码
    public function verify(){
        $Verify = new \Think\Verify();
        $Verify->fontSize = 20;
        $Verify->length   = 4;
        $Verify->useNoise = false;
        $verify->useCurve = false;
        $Verify->entry();
    }

    //加载css和js
    public function setStyle($css="",$js=""){
        $style = '';
        if(!empty($css)){
            if(is_array($css)){
                foreach($css as $val){
                    $style .= '<link rel="stylesheet" type="text/css" href="/xingc/Public/home/style/'.$val.'.css">';
                }
            }
            else{
                $style .= '<link rel="stylesheet" type="text/css" href="/xingc/Public/home/style/'.$css.'.css">';
            }
        }
        if(!empty($js)){
            if(is_array($js)){
                foreach($js as $val){
                    $style .= '<script src="/xingc/Public/home/js/'.$val.'.js"></script>';
                }
            }
            else{
                $style .= '<script src="/xingc/Public/home/js/'.$js.'.js"></script>';
            }
        }
        $this->assign('style',$style);
    }

    //列表页
    public function lst($cate_id,$page_num=8){
        $m_article = M('Article');
        if(!empty($cate_id)){
            $where['cate_id'] = array('in',implode(',', $cate_id));
        }
        $where['status'] = 1;
        $where['publish_time'] = array('lt',time());
        //分页
        $count = $m_article->where($where)->count();
        $Page       = new \Think\Page($count,$page_num);
        $Page->lastSuffix = false;
        $show       = $Page->hshow();

        $data = $m_article->where($where)->order('sort DESC,publish_time DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign(array(
            'show' => $show,
            'data' => $data
        ));
    }

    //单页
    // public function danye($cate_id){
    //     $id = I('get.id');
    //     $m_cate = M('Cate');
    //     //顶级栏目信息
    //     $top = $m_cate->field(array('id','name','contro_name'))->where(array('contro_name'=>CONTROLLER_NAME))->find();
    //     //栏目列表
    //     $cate_list = $m_cate->where(array('parent_id'=>$top['id']))->select();
    //     if(empty($cate_id)){
    //         $data = $m_cate->where(array('id'=>$cate_list[0]['id']))->find();
    //     }
    //     else{
    //         $data = $m_cate->where(array('id'=>$id))->find();
    //     }
    //     $data = $m_cate->where(array('id'=>$id))->find();
    //     $this->assign(array(
    //         'top' => $top,
    //         'cate_list' => $cate_list,
    //         'data' => $data
    //     ));
    // }
     //详情页
    public function details(){
        $id = I('get.id');
        $m_cate = D('Systemht/Cate');
        $m_article = M('Article');
        //顶级栏目信息
        $top = $m_cate->field(array('id','name','contro_name'))->where(array('contro_name'=>CONTROLLER_NAME))->find();
        //栏目列表
        $cate_list = $m_cate->getCate($top['id']);        //栏目列表

        $data = $m_article->where(array('status'=>1,'id'=>$id))->find();
        //分类信息
        $fenlei = M('Cate')->where(array('id'=>$data['cate_id']))->find();

        //相关产品
        if(CONTROLLER_NAME == 'Products'){
           $xiangguan = $m_article->where(array(
            'status' => 1,
            'publish_time' => array('lt',time()),
            'cate_id' => $fenlei['id']
            ))->order('publish_time DESC')->limit(3)->select();
           $this->assign('xiangguan',$xiangguan);
        }
        // var_dump($xiangguan);exit;
        //行业资讯
        $zixun = $m_article->where(array(
            'status' => 1,
            'publish_time' => array('lt',time()),
            'cate_id' => 11
            ))->order('publish_time DESC')->limit(8)->select();

        //上一篇
        $sql = 'select `id`,`name` from qiye_article where `id` =(
select max(id) from `qiye_article` where `status` = 1 and `id` < '.$id.' and `cate_id` = '.$data["cate_id"].')';
        $prev = $m_article->query($sql);

        //下一篇
        $sql = 'select `id`,`name` from qiye_article where `id` =(
select min(id) from `qiye_article` where `status` = 1 and `id` > '.$id.' and `cate_id` = '.$data["cate_id"].')';
        $next = $m_article->query($sql);

        //增加浏览次数
        $m_article->where(array('id',$data['id']))->setInc('pv');
        $this->assign(array(
            'top' => $top,
            'cate_list' => $cate_list,
            'data' => $data,
            'fenlei' => $fenlei,
            'prev' => $prev[0],
            'next' => $next[0],
            'zixun' => $zixun,
            ));
        // if($fenlei['type'] == 1){
        //     $this->setStyle('new_detailed');
        //     $this->display('Index/news_info');
        // }
        // else if($fenlei['type'] == 3){
        //     $this->setStyle('pro_detailed');
        //     $this->display('Index/pro_info');
        // }
        if(CONTROLLER_NAME == 'Products'){
            $this->setStyle('pro_detailed');
            $this->display('Index/pro_info');
        }
        else{
            $this->setStyle('new_detailed');
            $this->display('Index/news_info');
        }
    }

    public function getType(){
        $m_cate = D('Systemht/Cate');
        $m_article = M('Article');
        //顶级栏目信息
        $top = $m_cate->field(array('id','name','contro_name'))->where(array('contro_name'=>CONTROLLER_NAME))->find();

        $cate_list = $m_cate->getCate($top['id']);        //栏目列表
        $this->assign('top',$top);
        $this->assign('cate_list',$cate_list);
        //内容列表
        $id = I('get.id');
        if(!empty($id)){
            $cate_name = $m_cate->where(array('id'=>$id))->find();
            $this->assign('cate_id',$id);
            $this->assign('cate_name',$cate_name['name']);
            $cate_id = $m_cate->getCateId($id);
            if(empty($cate_id)){
                $cate_id = array($id);
            }
            //判断栏目类型
            switch ($cate_name['type']) {
                case '1':
                    $this->lst($cate_id,C('NEWS_PAGE'));
                    $this->setStyle('new');
                    $this->display('Index/news');
                    break;
                case '2':
                    $data = $m_cate->where(array('id'=>$id))->find();
                    $this->assign('data',$data);
                    $this->setStyle('about');
                    $this->display('Index/danye');
                    break;
                case '3':
                    $this->lst($cate_id,C('PIC_PAGE'));
                    $this->setStyle('pro');
                    $this->display('Index/pic');
                    break;
                // case '4':
                //     $this->lst(array($id),C('NEWS_PAGE'));
                //     $this->display('Index/news');
                //     break;
                default:
                    $this->lst($cate_id,C('NEWS_PAGE'));
                    $this->setStyle('new');
                    $this->display('Index/news');
                    break;
            }
        }
        else{
            //判断第一个栏目类型
            if($cate_list[0]['type'] != 2){
                $cate_id = $m_cate->getCateId($top['id']);
                switch ($cate_list[0]['type']) {
                    case '1':
                        $this->lst($cate_id,C('NEWS_PAGE'));
                        $this->setStyle('new');
                        $this->display('Index/news');
                        break;
                    case '3':
                        $this->lst($cate_id,C('PIC_PAGE'));
                        $this->setStyle('pro');
                        $this->display('Index/pic');
                        break;
                    // case '4':
                    //     $this->lst($cate_id,C('NEWS_PIC_PAGE'));
                    //     $this->display('Index/news');
                    //     break;
                    default:
                        $this->lst($cate_id,C('NEWS_PAGE'));
                        $this->setStyle('new');
                        $this->display('Index/news');
                        break;
                }
            }
            else{
                $data = $m_cate->where(array('id'=>$cate_list[0]['id']))->find();
                $this->assign('data',$data);
                $this->setStyle('about');
                $this->display('Index/danye');
            }
        }
    }
}